package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.service.ITemplateService;
import com.heima.common.dto.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-23
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {
    @Autowired
    private IApArticleContentService contentService;
    @Autowired
    @Lazy
    private ITemplateService templateService;


    /**
     * 保存文章
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        if(dto.getId()==null){
            //id为空，新增文章
            //保存文章表
            apArticle.setCreatedTime(new Date());
            this.save(apArticle);
            ApArticleContent content=new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            contentService.save(content);

            System.out.println(Thread.currentThread().getName()+"开始调用生成静态html服务");
            templateService.createHtml(apArticle,dto.getContent());
            System.out.println(Thread.currentThread().getName()+"生成静态html调用结束");
            return ResponseResult.okResult(apArticle.getId());
        }else{
            //id不为空，修改文章
            this.updateById(apArticle);
            //修改内容表
            LambdaUpdateWrapper<ApArticleContent> update=new LambdaUpdateWrapper<>();
            update.eq(ApArticleContent::getArticleId,dto.getId());
            update.set(ApArticleContent::getContent,dto.getContent());
            contentService.update(update);
            return ResponseResult.okResult(apArticle.getId());
        }
    }

    /**
     * 加载文章 0 加载修心1加载更多
     * @param dto
     * @param type
     * @return
     */
    @Override
    public ResponseResult loadArtical(ArticleHomeDto dto, int type) {
        IPage<ApArticle> page=new Page<>(1,dto.getSize());
        LambdaQueryWrapper<ApArticle> query=new LambdaQueryWrapper<>();
        if(dto.getChannelId()!=0){
            query.eq(ApArticle::getChannelId,dto.getChannelId());
        }
        if(type==0){
            query.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }else{
            query.lt(ApArticle::getPublishTime,dto.getMinTime());
        }
        query.eq(ApArticle::getIsDelete,false);
        query.eq(ApArticle::getIsDown,false);
        IPage<ApArticle> iPage = this.page(page, query);
        return ResponseResult.okResult(iPage.getRecords());
    }
    ;

    /**
     * 加载文章信息
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticalInfo(ArticleInfoDto dto) {
        ApArticle article = this.getById(dto.getArticleId());
        LambdaQueryWrapper<ApArticleContent> query=new LambdaQueryWrapper<>();
        query.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent articleContent = contentService.getOne(query);
        Map<String,Object> map=new HashMap<>();
        map.put("content",articleContent);
        map.put("config",article);
        return ResponseResult.okResult(map);
    }
}
